package com.dingup.api.Util;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dingup.api.model.sys.SysPermission;
import com.dingup.api.model.sys.SysUser;
import com.suyeer.cache.RedisUtil;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * Created by wancheng on 2018/7/16.
 */
public class RedisUtils extends RedisUtil {

    private static class LazyHolder {
        private static final RedisUtils ourInstance = new RedisUtils();
    }

    public static RedisUtils getInstance() {
        return LazyHolder.ourInstance;
    }

    private static final Integer USER_EXPIRE_TIME = 60 * 60 * 1 * 2;

    public static final String USER_ID = "userId";

    public static final String USER_NAME = "userName";

    public static final String TOKEN = "token";

    public static final String DATA_PERMISSION_DEPT = "dataPermissionDept";

    private static final String DATA_PERMISSION_MEMBER = "dataPermissionEmployee";

    private static final String DATA_PERMISSION = "data_permission";
    //redis中url缓存key
    private static final String PERMISSION_KEY = "PERMISSION";

    private static final String PERMISSION_URL = "permission_url";

    private static final String VERSION = "version";

    private static final String ORGANIZATIONAL_STRUCTURE = "ORGANIZATIONAL_STRUCTURE";

    public String getUserId(String userAccount) {
        return hget(userAccount, USER_ID);
    }
    public String getUserName(String userAccount) {
        return hget(userAccount, USER_NAME);
    }
    public String getUserId(HttpServletRequest request) {
        String userAccount = JwtTokenUtil.getUserAccountFromRequset(request);
        return hget(userAccount, USER_ID);
    }

    public void setUserId(String userAccount, String userId) {
        hset(userAccount, USER_ID, userId);
        expire(userAccount, USER_EXPIRE_TIME);
    }
    public void setUser(SysUser user) {
        hset(user.getUserAccount(), USER_ID, user.getUserId());
        hset(user.getUserAccount(), USER_NAME, user.getUserName());
        expire(user.getUserAccount(), USER_EXPIRE_TIME);
    }
    public void setToken(String userAccount, String token) {
        hset(userAccount, TOKEN, token);
    }

    public String getToken(String userAccount) {
        return hget(userAccount, TOKEN);
    }

    public void setDataPermission(String userAccount, String dataPermission) {
        hset(userAccount, DATA_PERMISSION, dataPermission);
    }

    public void getDataPermission(String userAccount) {
        hget(userAccount, DATA_PERMISSION);
    }

    public void setDataPermissionDept(String userAccount, List<Long> dataPermissionDept) {
        String value = "";
        value = JSONArray.toJSONString(dataPermissionDept);
        hset(userAccount, DATA_PERMISSION_DEPT, value);
    }

    public List<Long> getDataPermissionDept(String userAccount) {
        String data = hget(userAccount, DATA_PERMISSION_DEPT);
        List<Long> deptIds = JSONArray.parseArray(data, Long.class);
        return deptIds;
    }

    public List<Long> getDataPermissionDept(HttpServletRequest request) {
        String userAccount = JwtTokenUtil.getUserAccountFromRequset(request);

        return getDataPermissionDept(userAccount);
    }

    public void setDataPermissionEmployee(String userAccount, List<String> dataPermissionEmployee) {
        String value = "";
        value = JSONArray.toJSONString(dataPermissionEmployee);
        hset(userAccount, DATA_PERMISSION_MEMBER, value);
    }

    public List<String> getDataPermissionEmployee(String userAccount) {
        String data = hget(userAccount, DATA_PERMISSION_MEMBER);
        List<String> members = JSONArray.parseArray(data, String.class);
        return members;
    }

    public List<String> getDataPermissionEmployee(HttpServletRequest request) {
        String userAccount = JwtTokenUtil.getUserAccountFromRequset(request);
        return getDataPermissionEmployee(userAccount);
    }

    /**
     * 获取redis中的url
     *
     * @return
     */
    public List<SysPermission> getPermissionUrl() {
        String permissionJson = hget(PERMISSION_KEY, PERMISSION_URL);
        List<SysPermission> permissions = JSONObject.parseArray(permissionJson, SysPermission.class);
        return permissions;
    }

    public Integer getPermissionVersion() {
        String version = hget(PERMISSION_KEY, VERSION);
        if (StringUtils.isEmpty(version)) {
            return null;
        }
        return Integer.valueOf(version);
    }

    public void setPermissionUrl(List<SysPermission> permissions) {
        String permissionJson = JSONObject.toJSONString(permissions);
        String version = hget(PERMISSION_KEY, VERSION);
        if (StringUtils.isEmpty(version)) {
            version = "0";
        }
        hset(PERMISSION_KEY, VERSION, String.valueOf(Integer.valueOf(version) + 1));
        hset(PERMISSION_KEY, PERMISSION_URL, permissionJson);

    }

    /**
     * 组织架构缓存信息
     */
    public void setOrganizationalStructure(Map<String, String> organizationalStructure) {
        hmset(ORGANIZATIONAL_STRUCTURE, organizationalStructure);
    }

    public void setDeptUser(Long deptId, String userId) {
        String stringOfUserIds = hget(ORGANIZATIONAL_STRUCTURE, String.valueOf(deptId));
        List<String> userIds = JSONObject.parseArray(stringOfUserIds, String.class);
        userIds.add(userId);
        hset(ORGANIZATIONAL_STRUCTURE, String.valueOf(deptId), JSONArray.toJSONString(userIds));
    }

    public void removeDeptlUser(Long deptId, String userId) {
        String stringOfUserIds = hget(ORGANIZATIONAL_STRUCTURE, String.valueOf(deptId));
        List<String> userIds = JSONObject.parseArray(stringOfUserIds, String.class);
        userIds.remove(userId);

        hset(ORGANIZATIONAL_STRUCTURE, String.valueOf(deptId), JSONArray.toJSONString(userIds));
    }

    public Boolean getPermission(String userAccount, String permission) {
        String res = hget(userAccount, permission);
        if (StringUtils.isNotEmpty(res)) {
            try {
                return Boolean.valueOf(res);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * key是否需要刷新
     *
     * @param key
     * @return
     */
    public Boolean ifKeyNeedRefresh(String key) {
        Long exTime = ttl(key);
        if (exTime <= 60 * 30) {
            return true;
        } else {
            return false;
        }
    }

    public void refreshUserKey(String key) {
        expire(key, USER_EXPIRE_TIME);
    }

}
